package com.gupao.vip.bio;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

import org.apache.commons.io.IOUtils;

/**
 * 客户端-接收数据
 * @author dell
 *
 */
public class Receive implements Runnable {
	/**
	 * 输入流
	 */
	private DataInputStream dataInputStream = null;
	
	/**
	 * 线程标识
	 */
	private boolean isRunning = true;
	
	public Receive(){}
	
	public Receive(Socket client){
		try {
			dataInputStream = new DataInputStream(client.getInputStream());
		} catch (IOException e) {
			System.err.println(e);
			IOUtils.closeQuietly(dataInputStream);
			isRunning = false;
		}
	}
	
	/**
	 * 接收数据
	 * @return
	 */
	public String receiveMsg(){
		try {
			return dataInputStream.readUTF();
		} catch (IOException e) {
			System.err.println(e);
			IOUtils.closeQuietly(dataInputStream);
			isRunning = false;
		}
		return "";
	}
	
	@Override
	public void run() {
		while(isRunning){
			System.out.println(receiveMsg());
		}
		
	}

}
